﻿using LD.Controls;
using LD.Quartz;
using System;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LD.Host.Screw
{
    public partial class FrmMain : Form
    {
        private ReDrawUI ReDrawUI = new ReDrawUI();
        private ToolTip tooltips = new ToolTip();
        private FrmSubManager frmsubManager = new FrmSubManager();
        public static FrmMain instance;
        private static object locker = new object();
        public FrmMain()
        {
            InitializeComponent();
        }
        public FrmMain Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                            instance = new FrmMain();
                    }
                }
                return instance;
            }
        }
        private void UserChangeEvent(UserModel user)
        {
            this.tslab_user.Text = user.Name;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            FrmExit frmExit = new FrmExit();
            if (frmExit.ShowDialog() == DialogResult.Cancel)
            {
                e.Cancel = true;
                return;
            }
            else
                e.Cancel = false;

            WipHelper.Instance.Save();

            SciHelper.Instance.Stop();

            foreach (var item in TcpIpService.Instance.CommuniDic)
            {
                item.Value.ConnectionStateEvent -= TcpIpConnectionStateEvent;
            }

            ScrewManger.Instance.Stop();

            Task.Delay(50).Wait();

            Environment.Exit(0);
        }

        private void switchMenu_Click(object sender, EventArgs e)
        {
            var swBtn = (SwitchMenu)sender;
            if (swBtn.Tag != null)
                frmsubManager.Show(swBtn.Tag.ToString());
        }

        private void uiTimer_Tick(object sender, EventArgs e)
        {
            this.tslab_time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            UserHelper.Instance.Logout();

            GC.Collect();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(MainForm_Resize);
            ReDrawUI.ControlInitializeSize(this.panel_menu);

            sw_user.Checked = true;

            Regedit();
        }
        private void PartNoChangeEvent(string partNo)
        {
            sw_partNo.Text = partNo;
        }
        private void MainForm_Resize(object sender, EventArgs e)
        {
            ReDrawUI.ControlAutoSize(this.panel_menu);
        }
        private void sw_run_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void sw_pause_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void sw_stop_CheckedChanged(object sender, EventArgs e)
        {
        }
        private void MachineStatusChangedEvent(string status)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    switch (status)
                    {
                        case "Start":
                            tsl_machineModel.Text = "自动模式";
                            sw_run.Checked = true;
                            LogHelper.Instance.Auto("开启自动模式");
                            break;
                        case "Stop":
                            tsl_machineModel.Text = "手动模式";
                            sw_stop.Checked = true;
                            LogHelper.Instance.Auto("开启手动模式");
                            break;
                        case "Reset":
                            sw_run.Checked = false;
                            sw_stop.Checked = false;
                            sw_pause.Checked = false;
                            break;
                        case "Pause":
                            sw_pause.Checked = true;
                            break;
                    }
                }));
            }
        }

        public void SetToolTip()
        {
            this.tooltips.SetToolTip(this.sw_home, "主界面");
            this.tooltips.SetToolTip(this.sw_setting, "参数设置");
            this.tooltips.SetToolTip(this.sw_ccd, "相机设定");
            this.tooltips.SetToolTip(this.sw_alert, "报警信息");
            this.tooltips.SetToolTip(this.sw_chart, "报表信息");
            this.tooltips.SetToolTip(this.sw_log, "LOG查询");
            this.tooltips.SetToolTip(this.sw_picture, "图片查询");
            this.tooltips.SetToolTip(this.sw_user, "用户权限");
            this.tooltips.SetToolTip(this.sw_run, "启动");
            this.tooltips.SetToolTip(this.sw_pause, "暂停");
            this.tooltips.SetToolTip(this.sw_stop, "停止");
        }

        private void Regedit()
        {
            UserHelper.Instance.UserChangeEvent += UserChangeEvent;
            PartNoHelper.Instance.PartNoChangeEvent += PartNoChangeEvent;
            frmsubManager.Register("Main", null, null, panel_center, switchMenu_Click);

            this.sw_partNo.Text = PartNoHelper.Instance.CurrentPartNo?.Name;

            foreach (var item in TcpIpService.Instance.CommuniDic)
            {
                item.Value.ConnectionStateEvent += TcpIpConnectionStateEvent;
            }


            ModbusRtuHelper.Instance.Start();
        }

        private void TcpIpConnectionStateEvent(string name, Status status)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    if (!this.IsDisposed)
                    {
                        Color color = Color.Red;
                        if (status == Status.成功)
                            color = Color.White;

                        var controlIsExist = staStripMain.Items.Find(name, false).FirstOrDefault();
                        if (controlIsExist == null)
                        {
                            ToolStripStatusLabel lab_hd = new ToolStripStatusLabel();
                            lab_hd.Name = name;
                            lab_hd.Text = name;
                            lab_hd.BackColor = color;
                            staStripMain.Items.Add(lab_hd);
                        }
                        else
                        {
                            controlIsExist.BackColor = color;
                        }
                    }
                }));
            }
        }
        private void sw_run_Click(object sender, EventArgs e)
        {
            var sw = sender as SwitchMenu;
            sw.Checked = false;
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            //add
            string cron_8 = "0 0 8 * * ?";
            //wip信息保存
            QuartzUtil.AddJob<ExportWipJob>("ExportWipJob", cron_8);
            //抛料信息保存
            QuartzUtil.AddJob<ThrowMaterialJob>("ThrowMaterialJob", cron_8);
            //真空信息保存
            QuartzUtil.AddJob<VacuumJob>("VacuumJob", cron_8);
            //生产数据重置
            QuartzUtil.AddJob<ClearZeroJob>("ClearZeroJob", cron_8);
            //db delete
            QuartzUtil.AddJob<DeleteDBJob>("DeleteDBJob", cron_8);


        }
    }
}
